/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.user.login;

import java.io.Serializable;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;
import es.sonxurxo.androidrunner.view.web.session.AndroidRunnerSession;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class LoginPage extends BasePage {

	public LoginPage() {
		super();
		add(new SignInForm("loginform", new Model(new SimpleUser())));

		if (AndroidRunnerSession.get().isSignedIn()) {
            setResponsePage(AndroidRunnerApplication.get().getHomePage());
        }
	}
	
	public LoginPage(PageParameters pageParameters) {
		this();
	}
	
	public final class SignInForm extends Form {

		private static final long serialVersionUID = 7986802625710098835L;

		public SignInForm(String id, IModel model) {
            super(id, new CompoundPropertyModel(model));
            add(new TextField("username").setRequired(true));
            add(new PasswordTextField("password").setResetPassword(true));
        }

        /**
         * Called upon form submit. Attempts to authenticate the user.
         */
        @Override
		protected void onSubmit() {
        	
        	SimpleUser user = ((SimpleUser) getModel().getObject());
            String username = user.getUsername();
            String password = user.getPassword();
                
            if (AndroidRunnerSession.get().signIn(username, password)) {

//				if (!continueToOriginalDestination()) {
                	// Continue to the application home page.
                    setResponsePage(getApplication().getHomePage());

//              }

            } else {
            	error(getLocalizer().getString("loginPage.errors.invalidCredentials", LoginPage.this));
            }
//          

            // ALWAYS do a redirect, no matter where we are going to. The point is that the
            // submitting page should be gone from the browsers history.
            setRedirect(true);
        }
    }
	
	public static class SimpleUser implements Serializable {

		private static final long serialVersionUID = 3386918253102133179L;
		
		private String username;
    	private String password;

    	public String getUsername() {
    		return this.username;
    	}
    	
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	
    	public String getPassword() {
    		return this.password;
    	}
    	
    	public void setPassword(String password) {
    		this.password = password;
    	}
	}

	@Override
	protected String getTitle() {
		return getLocalizer().getString("loginPage.title", LoginPage.this);
	}

}
